import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class P05_Panel extends JPanel  implements MouseListener, MouseMotionListener, ActionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[36];
	
	public int man=0;
	
	private boolean[] buttons_pressed = new boolean[11];
	private int buttons_position=-1;
	
	public String value="0";
	
	private boolean moving=false;
	
	private Timer timer;
	private boolean long_pressed=false;
	
	public P05_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/05.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		
		imageList[0]=new ImageIcon(getClass().getResource("images/5/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/5/0.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/5/0_.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/5/1.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/5/1_.png")).getImage();
		imageList[5]=new ImageIcon(getClass().getResource("images/5/2.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/5/2_.png")).getImage();
		imageList[7]=new ImageIcon(getClass().getResource("images/5/3.png")).getImage();
		imageList[8]=new ImageIcon(getClass().getResource("images/5/3_.png")).getImage();
		imageList[9]=new ImageIcon(getClass().getResource("images/5/4.png")).getImage();
		imageList[10]=new ImageIcon(getClass().getResource("images/5/4_.png")).getImage();
		imageList[11]=new ImageIcon(getClass().getResource("images/5/5.png")).getImage();
		imageList[12]=new ImageIcon(getClass().getResource("images/5/5_.png")).getImage();
		imageList[13]=new ImageIcon(getClass().getResource("images/5/6.png")).getImage();
		imageList[14]=new ImageIcon(getClass().getResource("images/5/6_.png")).getImage();
		imageList[15]=new ImageIcon(getClass().getResource("images/5/7.png")).getImage();
		imageList[16]=new ImageIcon(getClass().getResource("images/5/7_.png")).getImage();
		imageList[17]=new ImageIcon(getClass().getResource("images/5/8.png")).getImage();
		imageList[18]=new ImageIcon(getClass().getResource("images/5/8_.png")).getImage();
		imageList[19]=new ImageIcon(getClass().getResource("images/5/9.png")).getImage();
		imageList[20]=new ImageIcon(getClass().getResource("images/5/9_.png")).getImage();
		imageList[21]=new ImageIcon(getClass().getResource("images/5/comma.png")).getImage();
		imageList[22]=new ImageIcon(getClass().getResource("images/5/00.png")).getImage();
		imageList[23]=new ImageIcon(getClass().getResource("images/5/01.png")).getImage();
		imageList[24]=new ImageIcon(getClass().getResource("images/5/02.png")).getImage();
		imageList[25]=new ImageIcon(getClass().getResource("images/5/03.png")).getImage();
		imageList[26]=new ImageIcon(getClass().getResource("images/5/04.png")).getImage();
		imageList[27]=new ImageIcon(getClass().getResource("images/5/05.png")).getImage();
		imageList[28]=new ImageIcon(getClass().getResource("images/5/06.png")).getImage();
		imageList[29]=new ImageIcon(getClass().getResource("images/5/07.png")).getImage();
		imageList[30]=new ImageIcon(getClass().getResource("images/5/08.png")).getImage();
		imageList[31]=new ImageIcon(getClass().getResource("images/5/09.png")).getImage();
		imageList[32]=new ImageIcon(getClass().getResource("images/5/x.png")).getImage();
		imageList[33]=new ImageIcon(getClass().getResource("images/5/x_.png")).getImage();
		imageList[34]=new ImageIcon(getClass().getResource("images/5/comma_.png")).getImage();
		imageList[35]=new ImageIcon(getClass().getResource("images/5/no.png")).getImage();
		
		
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
		buttons_pressed[0]=false;
		buttons_pressed[1]=false;
		buttons_pressed[2]=false;
		buttons_pressed[3]=false;
		buttons_pressed[4]=false;
		buttons_pressed[5]=false;
		buttons_pressed[6]=false;
		buttons_pressed[7]=false;
		buttons_pressed[8]=false;
		buttons_pressed[9]=false;
		buttons_pressed[10]=false;
		
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
				
		//buttons
		
		//7
		if(!buttons_pressed[7]){
			g.drawImage(imageList[15], 771, 600, null);
		}else{
			g.drawImage(imageList[16], 771, 600, null);
		}
		
		//8
		if(!buttons_pressed[8]){
			g.drawImage(imageList[17], 903, 600, null);
		}else{
			g.drawImage(imageList[18], 903, 600, null);
		}
				
		//9
		if(!buttons_pressed[9]){
			g.drawImage(imageList[19], 1035, 600, null);
		}else{
			g.drawImage(imageList[20], 1035, 600, null);
		}
			
		
		//4
		if(!buttons_pressed[4]){
			g.drawImage(imageList[9], 771, 703, null);
		}else{
			g.drawImage(imageList[10], 771, 703, null);
		}
		
		//5
		if(!buttons_pressed[5]){
			g.drawImage(imageList[11], 903, 703, null);
		}else{
			g.drawImage(imageList[12], 903, 703, null);
		}
		
		//6
		if(!buttons_pressed[6]){
			g.drawImage(imageList[13], 1035, 703, null);
		}else{
			g.drawImage(imageList[14], 1035, 703, null);
		}
		
		
		//1
		if(!buttons_pressed[1]){
			g.drawImage(imageList[3], 771, 806, null);
		}else{
			g.drawImage(imageList[4], 771, 806, null);
		}
				
		//2
		if(!buttons_pressed[2]){
			g.drawImage(imageList[5], 903, 806, null);
		}else{
			g.drawImage(imageList[6], 903, 806, null);
		}
		
		//3
		if(!buttons_pressed[3]){
			g.drawImage(imageList[7], 1035, 806, null);
		}else{
			g.drawImage(imageList[8], 1035, 806, null);
		}
		
		
		//0
		if(!buttons_pressed[0]){
			g.drawImage(imageList[1], 771, 909, null);
		}else{
			g.drawImage(imageList[2], 771, 909, null);
		}
		
		//x
		if(!buttons_pressed[10]){
			g.drawImage(imageList[32], 1035, 909, null);
		}else{
			g.drawImage(imageList[33], 1035, 909, null);
		}
		
		//numbers
		//22-32
		
		if(value.length()>10){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-11,1))], 292, 350, null);
		}else{
			g.drawImage(imageList[35], 292, 350, null);
		}
		
		if(value.length()>9){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-10).substring(0,1))], 404, 350, null);
		}else{
			g.drawImage(imageList[35], 404, 350, null);
		}
		
		if(value.length()>8){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-9).substring(0,1))], 557, 350, null);
		}else{
			g.drawImage(imageList[35], 557, 350, null);
		}
		
		if(value.length()>7){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-8).substring(0,1))], 670, 350, null);
		}else{
			g.drawImage(imageList[35], 670, 350, null);
		}
		
		if(value.length()>6){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-7).substring(0,1))], 783, 350, null);
		}else{
			g.drawImage(imageList[35], 783, 350, null);
		}
		
		if(value.length()>5){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-6).substring(0,1))], 936, 350, null);
		}else{
			g.drawImage(imageList[35], 936, 350, null);
		}
		
		if(value.length()>4){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-5).substring(0,1))], 1049, 350, null);
		}else{
			g.drawImage(imageList[35], 1049, 350, null);
		}
		
		if(value.length()>3){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-4).substring(0,1))], 1161, 350, null);
		}else{
			g.drawImage(imageList[35], 1161, 350, null);
		}
		
		if(value.length()>2){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-3).substring(0,1))], 1314, 350, null);
		}else{
			g.drawImage(imageList[35], 1314, 350, null);
		}
		
		if(value.length()>1){
			g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-2).substring(0,1))], 1427, 350, null);
		}else{
			g.drawImage(imageList[35], 1427, 350, null);
		}
		
		g.drawImage(imageList[22+Integer.valueOf(value.substring(value.length()-1).substring(0,1))], 1540, 350, null);
		
		//,
		if(value.length()>9){
			g.drawImage(imageList[21], 517, 481, null);
		}else{
			g.drawImage(imageList[34], 517, 481, null);
		}

		if(value.length()>6){
			g.drawImage(imageList[21], 895, 481, null);
		}else{
			g.drawImage(imageList[34], 895, 481, null);
		}

		if(value.length()>3){
			g.drawImage(imageList[21], 1274, 481, null);
		}else{
			g.drawImage(imageList[34], 1274, 481, null);
		}
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
				
		//buttons
		
		//120*76
		
		//7
		if(771<=e.getX() && e.getX()<=771+120 
				&& 600<=e.getY() && e.getY()<=600+76){
			buttons_pressed[7]=true;
			buttons_position=7;
		}
		
		//8
		if(903<=e.getX() && e.getX()<=903+120 
				&& 600<=e.getY() && e.getY()<=600+76){
			buttons_pressed[8]=true;
			buttons_position=8;
		}
		
		//9
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 600<=e.getY() && e.getY()<=600+76){
			buttons_pressed[9]=true;
			buttons_position=9;
		}
		
		//4
		if(771<=e.getX() && e.getX()<=771+120 
				&& 703<=e.getY() && e.getY()<=703+76){
			buttons_pressed[4]=true;
			buttons_position=4;
		}
		
		//5
		if(903<=e.getX() && e.getX()<=903+120 
				&& 703<=e.getY() && e.getY()<=703+76){
			buttons_pressed[5]=true;
			buttons_position=5;
		}
		
		//6
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 703<=e.getY() && e.getY()<=703+76){
			buttons_pressed[6]=true;
			buttons_position=6;
		}
		
		//1
		if(771<=e.getX() && e.getX()<=771+120 
				&& 806<=e.getY() && e.getY()<=806+76){
			buttons_pressed[1]=true;
			buttons_position=1;
		}
		
		//2
		if(903<=e.getX() && e.getX()<=903+120 
				&& 806<=e.getY() && e.getY()<=806+76){
			buttons_pressed[2]=true;
			buttons_position=2;
		}
		
		//3
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 806<=e.getY() && e.getY()<=806+76){
			buttons_pressed[3]=true;
			buttons_position=3;
		}
		
		//0
		if(771<=e.getX() && e.getX()<=771+255 
				&& 909<=e.getY() && e.getY()<=909+81){
			buttons_pressed[0]=true;
			buttons_position=0;
		}
		
		//x
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 909<=e.getY() && e.getY()<=909+76){
			buttons_pressed[10]=true;
			buttons_position=10;
		}
		
		timer = new Timer(700, this);
		long_pressed=false;
		timer.start();
		
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	timer.stop();
    	
    	if(menu_presseed[0] && 1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
			}
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				listener.onChangeValue(6, Long.valueOf(value));
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	//7
		if(771<=e.getX() && e.getX()<=771+120 
				&& 600<=e.getY() && e.getY()<=600+76){
			typeDigit(7);
		}
		
		//8
		if(903<=e.getX() && e.getX()<=903+120 
				&& 600<=e.getY() && e.getY()<=600+76){
			typeDigit(8);
		}
		
		//9
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 600<=e.getY() && e.getY()<=600+76){
			typeDigit(9);
		}
		
		//4
		if(771<=e.getX() && e.getX()<=771+120 
				&& 703<=e.getY() && e.getY()<=703+76){
			typeDigit(4);
		}
		
		//5
		if(903<=e.getX() && e.getX()<=903+120 
				&& 703<=e.getY() && e.getY()<=703+76){
			typeDigit(5);
		}
		
		//6
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 703<=e.getY() && e.getY()<=703+76){
			typeDigit(6);
		}
		
		//1
		if(771<=e.getX() && e.getX()<=771+120 
				&& 806<=e.getY() && e.getY()<=806+76){
			typeDigit(1);
		}
		
		//2
		if(903<=e.getX() && e.getX()<=903+120 
				&& 806<=e.getY() && e.getY()<=806+76){
			typeDigit(2);
		}
		
		//3
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 806<=e.getY() && e.getY()<=806+76){
			typeDigit(3);
		}
		
		//0
		if(771<=e.getX() && e.getX()<=771+255 
				&& 909<=e.getY() && e.getY()<=909+81){
			typeDigit(0);
		}
		
		//x
		if(1035<=e.getX() && e.getX()<=1035+120 
				&& 909<=e.getY() && e.getY()<=909+76){
			typeDigit(-1);
		}
    	
		buttons_position=-1;
    	buttons_pressed[0]=false;
		buttons_pressed[1]=false;
		buttons_pressed[2]=false;
		buttons_pressed[3]=false;
		buttons_pressed[4]=false;
		buttons_pressed[5]=false;
		buttons_pressed[6]=false;
		buttons_pressed[7]=false;
		buttons_pressed[8]=false;
		buttons_pressed[9]=false;
		buttons_pressed[10]=false;
		
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
    	
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
    
    private void typeDigit(int value){
    	if(this.value.length()<11){
    		if(value==-1){
    			if(this.value.length()==1){
    				this.value="0";
    			}else{
    				this.value=this.value.substring(0,this.value.length()-1);
    			}
    		}else{
    			this.value+=String.valueOf(value);
    		}
    	}else{
    		if(value==-1){
    			this.value=this.value.substring(0,this.value.length()-1);
    		}
    	}
    	
    	this.value=String.valueOf(Long.valueOf(this.value));
    }
    
    public void actionPerformed(ActionEvent e) {
    	
    	if(!long_pressed){
    		timer.stop();
    		timer = new Timer(200, this);
    		timer.start();
    		long_pressed=true;
    	}else{
    		int x = (int)MouseInfo.getPointerInfo().getLocation().getX();
    		int y = (int)MouseInfo.getPointerInfo().getLocation().getY();
    		
    		if(1035<=x && x<=1035+120 
					&& 909<=y && y<=909+76){
    			typeDigit(-1);
			}
    		
    		this.repaint();
    	}
    	
    }
    
}